<script setup lang="ts">
import { useRouter } from 'vue-router';
import { useMenuStore } from '@/stores/menu'
import { useUserStore } from '@/stores/user'

const router = useRouter();

const userStore = useUserStore();
const menuStore = useMenuStore();

/**
 * 导航栏 菜单点击
 * @param routerPath {string} 路由
 */
const navTabClick = (routerPath: string) => {
    menuStore.addTab(routerPath);
    router.push({ path: routerPath });
}

/**
 * 导航栏 菜单关闭
 * @param routerPath {string} 路由
 */
const navTabClose = (routerPath: string) => {
    let prevRouterPath = menuStore.closeTab(routerPath);
    router.push({ path: prevRouterPath || '/' });
}

/**
 * 页面全屏
 */
const switchFullScreen = () => {
    if (!document.fullscreenEnabled) { return false; }

    if (!document.fullscreenElement) {
        // 全屏
        document.documentElement.requestFullscreen();
    } else {
        // 退出全屏
        document.exitFullscreen();
    }
}

/** 退出登录 */
const onLogout = () => {
    userStore.removeLoginInfo();
    router.push({ path: '/login/' });
}
</script>

<template>
    <div class="nav-bar">
        <div class="nav-tabs">
            <div v-for="item in menuStore.tabsMenu" class="nav-tab-item"
                :class="{ active: item.routerPath === menuStore.tabsMenuActive }" @click="navTabClick(item.routerPath)">
                {{ item.title }}
                <el-icon class="icon-close" @click.stop="navTabClose(item.routerPath)">
                    <Close />
                </el-icon>
            </div>
        </div>
        <div class="nav-menus">
            <div class="nav-menu-item" @click="switchFullScreen">
                <el-icon>
                    <FullScreen />
                </el-icon>
            </div>
            <div class="nav-menu-item">
                <el-icon>
                    <Message />
                </el-icon>
            </div>
            <div class="admin-info">
                <el-popover placement="bottom" :width="240" trigger="click">
                    <template #reference>
                        <div class="info-box">
                            <el-avatar :size="25" src="@/assets/logo.svg" />
                            <span class="admin-name">{{ userStore.user.name }}</span>
                        </div>
                    </template>
                    <div class="popper-admin-box">
                        <div class="info">
                            <el-avatar :size="70" src="@/assets/logo.svg" />
                            <span class="name">{{ userStore.user.name }}</span>
                            <span>{{ userStore.user.lastLoginTime }}</span>
                        </div>
                        <div class="btn-box">
                            <el-button type="primary" plain>个人资料</el-button>
                            <el-button type="danger" plain @click="onLogout()">退出</el-button>
                        </div>
                    </div>
                </el-popover>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.nav-bar {
    display: flex;
    justify-content: space-between;
    height: 50px;
    margin: 16px 16px 0 0;

    .nav-tabs {
        display: flex;
        width: calc(100vw - 500px);
        height: 100%;

        .nav-tab-item {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0 20px;
            border-radius: 4px 4px 0 0;
            opacity: .7;
            cursor: pointer;
            user-select: none;

            .icon-close {
                margin-left: 4px;
                font-size: 12px;
            }

            &.active {
                background-color: var(--ha-content-bg-color);
                box-shadow: var(--el-box-shadow-light);
                opacity: 1;
            }

            &:hover {
                color: #000;
                opacity: 1;
            }
        }
    }

    .nav-menus {
        display: flex;
        align-items: center;
        height: 100%;
        background-color: var(--ha-content-bg-color);
        border-radius: var(--el-border-radius-base);
        box-shadow: var(--el-box-shadow-light);

        .nav-menu-item {
            display: flex;
            width: 40px;
            height: 100%;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }

        .admin-info {
            display: flex;
            height: 100%;
            padding: 0 10px;
            align-items: center;
            cursor: pointer;
            user-select: none;

            .info-box {
                display: flex;
                align-items: center;
            }
        }

        .admin-name {
            margin-left: 6px;
            white-space: nowrap;
        }
    }
}

.popper-admin-box {
    padding: 5px 15px;

    .info {
        display: flex;
        flex-direction: column;
        align-items: center;

        .name {
            margin-top: 15px;
            font-size: 20px;
        }
    }

    .btn-box {
        display: flex;
        justify-content: space-between;
        margin-top: 10px;
    }
}
</style>